<?php

	// Session
	session_start();
	
	// Nese vizitori eshte i identifikuar
	if (!isset($_SESSION['user_id'])) {
		header("Location: login.php");
	}
	
	// Integrimi i skedave sistemore
    include('../include/functions.inc.php');
	include('../include/template.class.php');
	
	// Project
	$PROJECT = setProject();
	
	
	// Ndyshoret primare
	define('TPL','../include/styles/'.$PROJECT['style'].'/members/books.html');
	define('UID',$_SESSION['user_id']);
	define('FILENAME',basename($_SERVER["PHP_SELF"]));
	
	
	// Connection
	dbOpen("comunity");
	
	
	// Inicializimi i shablonave
	$tmp = new Template(TPL,$PROJECT['lang']);
	
	
	// SPLASH SCREEN
	if (isset($_GET['splash'])) 
		splash($_GET['splash'], "../members/books.php");
	
		
	
	// INDEXING OPTIONS
	$SECTION = indexing("SELECT id, name FROM sections");
	$ACCESS = $tmp->indexing("access");
	
	
	// MENUES
	$tmp->setContent("CATEGORY",$tmp->menu("category","../members"));
	$tmp->setContent("MYMENU",$tmp->menu("mymenu",FILENAME));
	$tmp->setContent("FOOTMENU",$tmp->menu("foot"));
	
	
	// TOPBOX
	$query = mysql_query("SELECT id,title,pubdate FROM library 
						  WHERE author_id ='".UID."' ORDER BY counter DESC LIMIT 5");
       
	// Nese gjen me shume se 5 reshta ne arkive
	if (mysql_num_rows($query) > 4) {
		
		while ($row = mysql_fetch_assoc($query)) {
			
			$TOPBOOKS[] = array("id" => $row['id'],
							    "title" => $row['title'],
							    "pubdate" => setDate($row['pubdate'])
							 );  
		}
	}
	else 
		$TOPBOOKS = array();

	// TOPBOX
	$tmp->setContent("TOPBOOKS",$TOPBOOKS);
	

	// HELP
	$tmp->setContent("HELP",$tmp->xmlContent("howdropbook","help.xml"));

	
	// PAGE MANIPULATION
	$PAGE['query'] = "SELECT id FROM library WHERE author_id = '".UID."'";
	$PAGE = setPage(SID,$_GET['page'],FILENAME,$PAGE['query']);
	$tmp->setContent($PAGE);
	
	
	// LABELS
	$tmp->setLabels();


	// Nese gjen me se paku nje artikull per antarin
	if ($PAGE['rows'] > 0) {
			
		// Query
		$query = mysql_query("SELECT id,sid,volume,title,counter,access,pubdate 
								FROM library 
								WHERE author_id = '".UID."'
								ORDER BY pubdate DESC 
								LIMIT ".$PAGE['offset'].",".$PROJECT['booksPerPage']."");
				
			while ($BOOK = mysql_fetch_assoc($query)) {
				
				// BOOK VARS
				if (empty($BOOK['volume'])) { 
					
					$BOOK['volume'] = $BOOK['title'];
					$BOOK['title'] = NULL;
				}
				
				$BOOK['date'] = setDate($BOOK['pubdate']);
				$BOOK['curValue'] = ratingValue($BOOK['id']);
				$BOOK['access'] = $ACCESS[$BOOK['access']];
				$BOOK['section'] = $SECTION[$BOOK['sid']];
				$BOOK['comments'] = mysql_num_rows(mysql_query("SELECT id FROM comments WHERE book_id='".$BOOK['id']."'"));
				$BOOKS[] = $BOOK;
			}
		
		// Krijo fushen boshe per eleiminimin e blokt te porosis
		$MESSAGE = array();
	}
	// Nese nuk eshte gjetur asnje resht ne arkive
	else {
		
		// LIST
		$BOOKS = array();	
		
		// Krijo bllokun e porosis
		$MESSAGE = $tmp->xmlContent('nobooks');
	}

	// Bloku i porosis
	$tmp->setContent("MESSAGE",$MESSAGE);
	
	// Lista e artikujve
	$tmp->setContent("FORM",$BOOKS);
		
	// Dhe cdo gje ka nje fund, trego faqen						  
	echo $tmp->vorlage;

?>